package com.tzbank.wisdom.override;

import java.lang.reflect.Parameter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

import com.tzbank.wisdom.service.BuriedActionService;

@ControllerAdvice
public class ResponseBodyOverride implements ResponseBodyAdvice<Map<String, Object>> {

	@Autowired
	private BuriedActionService buriedActionService;

	@Override
	public boolean supports(MethodParameter arg0, Class<? extends HttpMessageConverter<?>> arg1) {
		return true;
	}

	@Override
	public Map<String, Object> beforeBodyWrite(Map<String, Object> result, MethodParameter methodParameter,
			MediaType arg2, Class<? extends HttpMessageConverter<?>> arg3, ServerHttpRequest serverHttpRequest,
			ServerHttpResponse arg5) {
		ServletServerHttpRequest sshRequest = (ServletServerHttpRequest) serverHttpRequest;
		HttpServletRequest request = sshRequest.getServletRequest();
		String url = request.getRequestURL().toString();
		String iParam = result.toString();

		Parameter[] parameters = methodParameter.getMethod().getParameters();
	
		

		for (Parameter parameter : parameters) {
			
			System.out.println(parameter.getAnnotations());
		}

		System.out.println(url);
		System.out.println(iParam);

		new Thread(new Runnable() {

			@Override
			public void run() {
				buriedActionService.buriedAction("", "", "");
			}

		}).start();

		return result;
	}

}
